package wawi.actions;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import wawi.db.DbConnector;
import wawi.util.Kategorie;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ArtikelListe extends ActionSupport
{
	private ArrayList<ArrayList<String>> aaData = new ArrayList<ArrayList<String>>();

	public ArrayList<ArrayList<String>> getAaData() {
		return aaData;
	}

	public void setAaData(ArrayList<ArrayList<String>> aaData) {
		this.aaData = aaData;
	}
	
	public String execute()
	{
		ArrayList<Artikel> artikel = DbConnector.getInstance().getArtikel();
		
		for (Artikel tmp : artikel)
		{
			ArrayList<String> row = new ArrayList<String>();
			row.add(tmp.getHersteller());
			row.add(tmp.getTextKurz());
			row.add(Kategorie.getKategorien().get(tmp.getKategorie()));
			row.add(tmp.getArtNr()+"");					
			row.add(String.format("<a href=\"Artikel_bearbeiten.action?artnr=%d\">Bearbeiten</a>", tmp.getArtNr()));
			
			aaData.add(row);
		}
		
		return ActionSupport.SUCCESS;
	}
	
	public String addPositionListe()
	{
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get( ServletActionContext.HTTP_REQUEST);
		
		ArrayList<Artikel> artikel = DbConnector.getInstance().getArtikel();
		
		for (Artikel tmp : artikel)
		{
			ArrayList<String> row = new ArrayList<String>();
			row.add(tmp.getHersteller());
			row.add(tmp.getTextKurz());
			row.add(tmp.getArtNr()+"");			
			row.add(String.format("<a href=\"Bestellung_addPosition.action?addArtikel=%d&recid=%s\">Hinzuf&uuml;gen</a>", tmp.getArtNr(), request.getParameter("recid")));
			
			aaData.add(row);
		}
		
		return ActionSupport.SUCCESS;
	}
}
